package com.uniquesoft.excelreader;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import com.uniquesoft.common.CommonUtils;
import com.uniquesoft.common.SourceFileLoader;

public class ZipHandler {

	private final static Logger logger = Logger.getLogger(ZipHandler.class.getName());
	private String path;
	private String folderPath;

	public ZipHandler(String path) {
		super();
		this.path = path;
	}
    
	@SuppressWarnings("all")
	public void zipDecompressing(String path) throws IOException {  
		
	    long startTime=System.currentTimeMillis();  

    	File zipFile = new File(path);
    	String parentPath = zipFile.getParent(); //输出路径（文件夹目录） 
    	
        //追加该文件名到解压路径 
        String fileName = zipFile.getName();  
        if (!CommonUtils.checkNull(fileName))  
        {  
            fileName = fileName.substring(0, fileName.lastIndexOf("."));  
        }  
        this.folderPath = parentPath + File.separator + fileName;  
        //创建解压缩文件保存的路径  
        File unzipFileDir = new File(this.folderPath);  
        if (!unzipFileDir.exists() || !unzipFileDir.isDirectory())  
        {  
            unzipFileDir.mkdirs();  
        }  
          
        //开始解压  
        ZipEntry entry = null;  
        String entryFilePath = null, entryDirPath = null;  
        File entryFile = null, entryDir = null;  
        int index = 0, count = 0, bufferSize = 1024;  
        byte[] buffer = new byte[bufferSize];  
        BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
        ZipFile zip = new ZipFile(zipFile);  
        Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();  
        //循环对压缩包里的每一个文件进行解压       
        while(entries.hasMoreElements())  
        {  
            entry = entries.nextElement();  
            //构建压缩包中一个文件解压后保存的文件全路径  
            entryFilePath = this.folderPath + File.separator + entry.getName();  
            //构建解压后保存的文件夹路径  
            index = entryFilePath.lastIndexOf(File.separator);
            if (index != -1)  
            {  
                entryDirPath = entryFilePath.substring(0, index);  
            }  
            else  
            {  
                entryDirPath = "";  
            }             
            entryDir = new File(entryDirPath);  
            //如果文件夹路径不存在，则创建文件夹  
            if (!entryDir.exists() || !entryDir.isDirectory())  
            {  
                entryDir.mkdirs();  
            }  
            //创建解压文件  
            entryFile = new File(entryFilePath); 
            
            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if(entryFile.isDirectory()){  
                continue;  
            }  
            
            if (entryFile.exists())  
            {  
                //检测文件是否允许删除，如果不允许删除，将会抛出SecurityException  
                SecurityManager securityManager = new SecurityManager();  
                securityManager.checkDelete(entryFilePath);  
                //删除已存在的目标文件  
                entryFile.delete();   
            }  
              
            //写入文件  
            bos = new BufferedOutputStream(new FileOutputStream(entryFile));  
            bis = new BufferedInputStream(zip.getInputStream(entry));  
            while ((count = bis.read(buffer, 0, bufferSize)) != -1)  
            {  
                bos.write(buffer, 0, count);  
            }  
            bos.flush();  
            bos.close();              
        }  
	  
	    long endTime=System.currentTimeMillis();  
	    logger.info("耗费时间： "+(endTime-startTime)+" ms");  
	} 
  
    public ArrayList<Map<Integer, Map<Integer, Object>>> resolveZipFile() throws Exception { 
    	
    	this.zipDecompressing(this.path);
    	
    	logger.info("解压文件夹路径：" + this.folderPath);  
    	
    	ArrayList<Map<Integer, Map<Integer, Object>>> mapList = SourceFileLoader.loadFolder(this.folderPath);
    	
    	return mapList;
    }  
}
